package com.cjj.common.framework.conf;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.cjj.common.framework.annotation.EnableTaskSwitch;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @author DoyuTu
 */
@Configuration
@EnableScheduling
@EnableTaskSwitch
public class SchedulingConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskScheduler());
    }

    @Bean(destroyMethod="shutdown")
    public ExecutorService taskScheduler() {
        return new ScheduledThreadPoolExecutor(10, new ThreadFactoryBuilder().setNameFormat("scheduled-pool-%d").build());
    }

}
